import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/global/color_config.dart';
import 'package:flutter_app/login/event/login_event.dart';
import 'package:flutter_app/login/widget/login_widget.dart';
import 'package:flutter_app/login/widget/register_widget.dart';
import 'package:flutter_app/util/event_util.dart';

class LoginWidget extends StatefulWidget {
  const LoginWidget({super.key});

  @override
  LoginPage createState() => LoginPage();
}

class LoginPage extends State<LoginWidget> {
  var eventData;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (Platform.isAndroid) {
      SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
          statusBarColor: Colors.transparent,
          statusBarIconBrightness: Brightness.light);
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }

    double topHeight = MediaQuery.of(context).size.height * 0.3;
    double bottomHeight = MediaQuery.of(context).size.height * 0.7;

    PageController controller = PageController(
      initialPage: 0, //用来配置PageView中默认显示的页面 从0开始
      keepPage: true, //为true是保持加载的每个页面的状态
    );
    eventData = EventUtil.getEvent.on<LoginEvent>().listen((event) {
      LoginEvent loginEvent = event;
      controller.jumpToPage(loginEvent.loginType);
    });

    return Scaffold(
      backgroundColor: ColorConfig.themeColor,
      body: Column(
        children: [
          ///头部widget
          Container(
            color: ColorConfig.themeColor,
            width: double.infinity,
            height: topHeight,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  width: 50,
                  height: 50,
                  margin: const EdgeInsets.only(left: 0, top: 32),
                  padding: const EdgeInsets.all(15),
                  child: InkWell(
                    child: const Image(
                      image: AssetImage("images/icon_login_close.png"),
                    ),
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(top: 30),
                  alignment: Alignment.center,
                  child: const Image(
                    width: 104,
                    height: 75,
                    image: AssetImage("images/ic_splash.png"),
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(top: 24),
                  alignment: Alignment.center,
                  child: const Text(
                    "欢迎使用",
                    style: TextStyle(
                        fontWeight: FontWeight.w300,
                        color: Colors.white,
                        fontSize: 18,
                        decoration: TextDecoration.none),
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(top: 4),
                  alignment: Alignment.center,
                  child: const Text(
                    "本APP由Zhangjc独立开发",
                    style: TextStyle(
                        fontWeight: FontWeight.w100,
                        color: Colors.white,
                        fontSize: 10,
                        decoration: TextDecoration.none),
                  ),
                ),
              ],
            ),
          ),

          Stack(
            textDirection: TextDirection.ltr,
            alignment: Alignment.topCenter,
            children: [
              ///底部Widget
              Container(
                color: Colors.white,
                width: double.infinity,
                height: bottomHeight,
                child: PageView(
                  scrollDirection: Axis.horizontal,
                  allowImplicitScrolling: true,
                  pageSnapping: true,
                  controller: controller,
                  children: const [
                    LoginPageViewWidget(),
                    RegisterPageViewWidget()
                  ],
                ),
              ),

              ///中间圆弧
              Container(
                color: Colors.transparent,
                width: double.infinity,
                height: 80,
                child: ClipPath(
                  clipper: LoginBgCard(),
                  child: Container(
                    width: double.infinity,
                    height: 80,
                    color: ColorConfig.themeColor,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    eventData.cancel();
  }
}

class LoginBgCard extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var path = Path();
    path.lineTo(0, 80);
    path.quadraticBezierTo(size.width / 2, 0, size.width, 80);
    path.lineTo(size.width, 0);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return false;
  }
}
